package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试MQ  发消息
 *
 */
@Api(tags = "Mq测试")
@RestController
@RequestMapping("/api/mq")
@Slf4j
public class MQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitService rabbitService;


    //测试第一个发消息
    @ApiOperation(" //测试第一个发消息")
    @GetMapping("/sendMessage")
    public Result sendMessage(){
        //交换机
        rabbitTemplate.convertAndSend(
                "sendTestExchange","sendTestRoutingKey","111");
        return Result.ok();

    }
    //消息消费者 应答模式 发消息测试
    @ApiOperation(" //测试发送Ack消息")
    @GetMapping("/sendAckMessage")
    public Result sendAckMessage(){
        //交换机
        rabbitTemplate.convertAndSend(
                "sendTestExchange","sendTestRoutingKey","111");
        return Result.ok();

    }
    //发消息   测试交换机应答  测试队列应答
    @ApiOperation(" //测试交换机应答测试队列应答")
    @GetMapping("/sendTestAckMessage")
    public Result sendTestAckMessage(){
        rabbitService.sendMessage(
                "exchange.confirm","routingKey111.confirm","123");

        return Result.ok();
    }

    //
    @ApiOperation("测试死信队列来完成延迟消息的发送")
    @GetMapping("/sendDeadLetterMessage")
    public Result sendDeadLetterMessage(){

        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,
                DeadLetterMqConfig.routing_dead_1,"11213131",msg -> {
                        //设置死信的 ttl 的过期时间  毫秒
                       log.info("发送死信队列来完成延迟消息");
                        msg.getMessageProperties().setExpiration("5000");
                        return msg;
                });

        return Result.ok();
    }
    //
    @ApiOperation("测试延迟插件发送延迟消息")
    @GetMapping("/sendDelayMessage")
    public Result sendDelayMessage(){

        rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,
                "我是测试延迟消息的消息",message -> {
                        //设置延迟的时间  毫秒    5秒
                        log.info("刚刚发送延迟消息");
                        message.getMessageProperties().setDelay(5000);
                        return message;
                });

        return Result.ok();
    }

}
